package cn.practice.chat.security;

import cn.practice.chat.feign.UserApiFeign;
import cn.practice.chat.lang.Result;
import cn.practice.chat.response.user.UserPasswordResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserDetailServiceImpl
 * @Description:
 * @Author A-TAO
 * @Date 2022/10/30
 * @Version V1.0
 **/
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserApiFeign userDetailFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Result<UserPasswordResult> result = userDetailFeign.getUserDetail(username);
        UserPasswordResult response = result.getData();
        if(ObjectUtils.isEmpty(response)){
            throw new UsernameNotFoundException("用户名或密码不正确");
        }
        return new AccountUser(response.getId(), response.getUsername(), response.getPassword(), null);
    }
}
